package cs2340.g21.wms;

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * A custom array adapter to allow us to take info from item objects
 * and put them into a list (for things like search results, user's item lists, etc)
 * @author Alexander
 * @version 1.0
 */
public class ItemAdapter extends ArrayAdapter<Item>{
    
    private static final String LOG_TAG = "debugger";
    Context context;
    int layoutResourceId;
    Item[] items = null;
    
    public ItemAdapter(Context context, int layoutResourceId, Item[] items){
        super(context, layoutResourceId, items);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.items = items;
    }
    
    /**
     * This gets called once per element in items and defines a row of the list
     * @param position current index in the array
     * @param convertView 
     * @param parent
     * @return the row with proper info filled out
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View row = convertView;
        ItemHolder holder = null;
        
        if(row == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ItemHolder();
            holder.img = (ImageView)row.findViewById(R.id.itemImg);
            holder.name = (TextView)row.findViewById(R.id.itemName);
            holder.date = (TextView)row.findViewById(R.id.itemDate);
            holder.category = (TextView)row.findViewById(R.id.itemCat);
            row.setTag(holder);
        }else{
            holder = (ItemHolder)row.getTag();
        }
            //Log.i(LOG_TAG, "making " + position);
            Item item = items[position];
            holder.name.setText(item.getName());
            if (item.getPicture() != null) {
            	holder.img.setImageBitmap(item.getPicture().getBitmap());
            }
            int month = item.getDate().get(Calendar.MONTH) + 1;
            holder.date.setText(item.getDate().get(Calendar.DAY_OF_MONTH)+ "/" + month);
            if(item.getCategory() == Category.LOST){
                holder.category.setText("L");
            }else if(item.getCategory() == Category.FOUND){
                holder.category.setText("F");
            }else if(item.getCategory() == Category.DONATE){
                holder.category.setText("D");
            }

        return row;
        
    }
    
    
    /**
     * Static class to hold each item's relevant info
     */
    static class ItemHolder{
        ImageView img;
        TextView name;
        TextView date;
        TextView category;
    }
    
    

}
